package com.example.demo.mapper;

import com.example.demo.model.AppSetting;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Optional;

@Mapper
public interface AppSettingMapper {

    @Select("SELECT id, config_key, config_value, last_updated FROM app_setting")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "configKey", column = "config_key"),
            @Result(property = "configValue", column = "config_value"),
            @Result(property = "lastUpdated", column = "last_updated")
    })
    List<AppSetting> findAll();

    @Select("SELECT * FROM app_setting WHERE config_key = #{configKey}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "configKey", column = "config_key"),
            @Result(property = "configValue", column = "config_value"),
            @Result(property = "lastUpdated", column = "last_updated")
    })
    Optional<AppSetting> findByConfigKey(String configKey);

    @Insert("INSERT INTO app_setting (config_key, config_value, last_updated) " +
            "VALUES (#{configKey}, #{configValue}, #{lastUpdated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertConfig(AppSetting config);

    @Update("UPDATE app_setting SET config_value = #{configValue}, last_updated = #{lastUpdated} " +
            "WHERE config_key = #{configKey}")
    int updateConfig(AppSetting config);
}